package com.building.hcrm.base;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.ComFunc;
import com.building.hcrm.common.HttpHelper;
import com.building.hcrm.model.SysUser;

import android.content.Context;

public class BaseServiceProxy
{
	protected Context _context;

	protected Boolean ValidateResult(String strResult) throws Exception
	{
		Boolean bReturn = false;
		if (null != strResult && 0 < strResult.length())
		{
			bReturn = true;
		}
		return bReturn;
	}
	
	protected List<String> ProcessResult(String strResult , String... strParams) throws JSONException
	{
		List<String> re = null;
		
		if(null != strParams && strParams.length>0)
		{
			JSONObject jobj = new JSONObject(strResult);
			
			re = new ArrayList<String>();
			
			for(String item : strParams)
			{
				re.add(jobj.getString(item));
			}
		}
		
		return re;
	}
	
	public Date GetServerTime() throws Exception
	{
		Date result = new Date();
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "ServerTime",
				null);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "ServerTimeResult");
			for(String str : lstRe)
			{
				result = ComFunc.GetDateByString(str, ComConst.DATE_TIME_FORMAT);
			}
		}
		return result;
	}
}
